package com.suzhe.ymall.common.config;

import com.suzhe.ymall.common.interceptor.FeignRequestInterceptor;
import com.suzhe.ymall.common.interceptor.RestTemplateAuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

@Configuration
@Import({FeignRequestInterceptor.class})
public class HttpAuthConfig {
    @Autowired
    RestTemplateAuthInterceptor restTemplateAuthInterceptor;

    @Bean
    RestTemplateAuthInterceptor restTemplateAuthInterceptor(){
        return new RestTemplateAuthInterceptor();
    }

    @Bean //向程序的ioc注入一个bean: restTemplate
    @LoadBalanced //表明这个restRemplate开启负载均衡的功能
    RestTemplate restTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        // 把自定义的ClientHttpRequestInterceptor添加到RestTemplate，可添加多个
        restTemplate.setInterceptors(Collections.singletonList(restTemplateAuthInterceptor));
        return restTemplate;
    }

}
